package com.jkt.rms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;

import com.jkt.rms.businessservice.ResourceRoleBusinessService;
import com.jkt.rms.cache.RMSCache;
import com.jkt.rms.commons.utils.RMSException;
import com.jkt.rms.commons.utils.ServiceLocator;
import com.jkt.rms.vo.ResourceRoleVO;


public class ResourceRoleController extends AbstractMultiActionController {

	private static final Logger LOG = Logger.getLogger(ResourceRoleController.class);
	private ResourceRoleBusinessService resourceRoleBusinessService;
	private RMSCache cache;
	/**
	 * 
	 * @param request
	 * @param response
	 * @param ResourceRoleVO
	 * @return
	 */
	 public ModelAndView addRole(HttpServletRequest request,HttpServletResponse response, ResourceRoleVO resourceRoleVO) {
		boolean isRoleExist = false;
		getBusinessService();
		validate(resourceRoleVO);
		if (errors != null && errors.getAllErrors() != null
				&& errors.getAllErrors().size() > 0) {
			return new ModelAndView("resourceRoleAddSearch","resourceRole", resourceRoleVO).addObject("errors", errors);
		} else {
			try {
				isRoleExist = resourceRoleBusinessService.saveRole(resourceRoleVO);
				if (isRoleExist) {
					resourceRoleVO.setMessage("resourceRole.already.exists");
				}else {
					resourceRoleVO=new ResourceRoleVO();
					resourceRoleVO.setMessage("resourceRole.success.msg");
				}
				getCache();
				cache.refersh(RMSCache.RESOURVCEROLECACHE);
			} catch (RMSException e) {
				LOG.error("Error Occurs in add Resource Role :", e.getCause());
				errors.reject(e.getBusinessCode());
			}catch(Exception ex){
				LOG.error("Exception Occurs in add Resource Role:" + ex.getCause());
				errors.reject("error.application");
			}
			
			return new ModelAndView("resourceRoleAddSearch","resourceRole", resourceRoleVO).addObject("errors", errors);
		}

	}
	 /**
	  * This method update and save the role
	  * @param request
	  * @param response
	  * @param ResourceRoleVO
	  * @return
	  */
	
	public ModelAndView updateRole(HttpServletRequest request,HttpServletResponse response, ResourceRoleVO resourceRoleVO) {
		boolean isVendorExist = false;
		getBusinessService();
		validate(resourceRoleVO);
		if (errors != null && errors.getAllErrors() != null
				&& errors.getAllErrors().size() > 0) {
			return new ModelAndView("updateResourceRole", "resourceRole",resourceRoleVO).addObject("errors", errors);
		} else {
			try {
				isVendorExist = resourceRoleBusinessService.updateRole(resourceRoleVO);
				if (isVendorExist) {
					resourceRoleVO.setMessage("resourceRole.already.exists");
				}else {
					resourceRoleVO.setMessage("resourceRole.update.msg");
				}
				getCache();
				cache.refersh(RMSCache.RESOURVCEROLECACHE);
			} catch (RMSException e) {
				LOG.error("Error Occurs in updating Resource Role :", e.getCause());
				errors.reject(e.getBusinessCode());
			}
			catch(Exception ex){
				LOG.error("Error Occurs in updating Resource Role :", ex.getCause());
				errors.reject("error.application");
			}
			return new ModelAndView("updateResourceRole", "resourceRole", resourceRoleVO).addObject("errors", errors);
		}

	}

	/**
	 * This method search all the result of Roles
	 * @param request
	 * @param response
	 * @return
	 * @throws RMSException
	 */
	 public ModelAndView findAllRoles(HttpServletRequest request,HttpServletResponse response)throws RMSException {
		getBusinessService();
		ResourceRoleVO resourceRoleVO =  new ResourceRoleVO();
		List<ResourceRoleVO> listOfRoleVO=null;
		try{
		listOfRoleVO = resourceRoleBusinessService.findAllRoles();
		if(listOfRoleVO == null || listOfRoleVO.size() == 0){
			resourceRoleVO.setMessage("no.record.found");
		}
		resourceRoleVO.setSearchMethod("findAllRoles");
		}catch(RMSException ex){
			LOG.error("Error Occurs in find All Resource Role :", ex.getCause());
			errors.reject(ex.getBusinessCode());
		}catch(Exception ex){
			LOG.error("Exception Occurs in find All Resource Role:" + ex.getCause());
			errors.reject("error.application");
		}
		return new ModelAndView("resourceRoleAddSearch", "resourceRole", resourceRoleVO).addObject("searchResult", listOfRoleVO).addObject("errors", errors);
	}
 
	 /**
     * This method search the role based on search criteria.
     * @param request
     * @param response
     * @param ResourceRoleVO
     * @return
     * @throws RMSException
     */
	public ModelAndView searchRoles(HttpServletRequest request, HttpServletResponse response, ResourceRoleVO resourceRoleVO){
		getBusinessService();
		ResourceRoleVO requestSerachVO=new ResourceRoleVO();
		ResourceRoleVO responseSerachVO=new ResourceRoleVO();
		List<ResourceRoleVO> listOfRoleVO = null;
		try{
		requestSerachVO.setRoleCode(resourceRoleVO.getSearchRoleCode());
		requestSerachVO.setRoleName(resourceRoleVO.getSearchRoleName());
		listOfRoleVO = resourceRoleBusinessService.findByCriteria(requestSerachVO);
		responseSerachVO.setSearchRoleCode(resourceRoleVO.getSearchRoleCode());
		responseSerachVO.setSearchRoleName(resourceRoleVO.getSearchRoleName());
		if(listOfRoleVO == null ) {
			responseSerachVO.setMessage("criteria.not.entered");
		} else if (listOfRoleVO.size() == 0){
			responseSerachVO.setMessage("no.record.found");
		}
		responseSerachVO.setSearchMethod("searchRoles");
		}catch(RMSException ex){
			LOG.error("Error Occurs in search Resource Role :", ex.getCause());
			errors.reject(ex.getBusinessCode());
		}catch(Exception ex){
			LOG.error("Exception Occurs in search Resource Role:" + ex.getCause());
			errors.reject("error.application");
		}
		finally{
			requestSerachVO=null;	
		}
		return new ModelAndView("resourceRoleAddSearch", "resourceRole", responseSerachVO).addObject("searchResult", listOfRoleVO).addObject("errors", errors);
	}

	/**
	 * This method load the Resource Role Page.
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView loadResourceRole(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResourceRoleVO resourceRoleVO = null;
		try {
			resourceRoleVO=new ResourceRoleVO();
			} catch (Exception e) {
		    LOG.error("Error occurs while loading Client Role Page:"+e.getCause());
			errors.reject("error.application");
			}
			return new ModelAndView("resourceRoleAddSearch","resourceRole", resourceRoleVO).addObject("errors", errors);
	}
	
	
		/**
	 * This method load the edit Resource Role Page.
	 * @param request
	 * @param response
	 * @return ModelAndView
	 * @throws Exception
	 */
	public ModelAndView loadEditResourceRole(HttpServletRequest request,
			HttpServletResponse response,ResourceRoleVO resourceRoleVO) throws Exception {
		String searchMethod=resourceRoleVO.getSearchMethod();
		String searchRoleName=resourceRoleVO.getSearchRoleName();
		String searchRoleCode=resourceRoleVO.getSearchRoleCode();
		getCache();
		resourceRoleVO=cache.getResourceRole(resourceRoleVO.getRoleName());
		resourceRoleVO.setSearchMethod(searchMethod);
		resourceRoleVO.setSearchRoleName(searchRoleName);
		resourceRoleVO.setSearchRoleCode(searchRoleCode);
		return new ModelAndView("updateResourceRole","resourceRole",resourceRoleVO);
	}
	
	/**
	 * Setter method to set business service.
	 */
	private void getBusinessService() {
		resourceRoleBusinessService = ServiceLocator.getServiceLocator().locate("resourceRoleBusinessService");
	}
	
	private RMSCache getCache() {
		cache = ServiceLocator.getServiceLocator().locate("cacheService");
		return cache;
	}
}
